nanopyx.core.analysis.estimate_shift

 1import numpy as np
 2from scipy.optimize import minimize
 3
 4from ..transform.interpolation_bicubic import interpolate
 5
 6class GetMaxOptimizer(object):
 7    """
 8    Class GetMaxOptimizer, used to extract the maximum value from a cross correlation matrix with subpixel precision.
 9    """
10
11    def __init__(self, slice_ccm) -> None:
12        """
13        Creates an instance of GetMaxOptimizer.
14        :param slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel
15        precision.
16        """
17        self.slice_ccm = slice_ccm
18
19    def get_interpolated_px_value(self, coords):
20        """
21        Method to be used for calculating the interpolated values of cross correlation matrices.
22        :param coords: tuple of coordinates.
23        :return: float; value of cross correlation matrix at given coordinates.
24        For minimizer reasons -> negatives values become positive and positive become negative.
25        """
26        return -interpolate(self.slice_ccm, coords[1], coords[0])
27
28    def get_max(self):
29        """
30        Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.
31        :return: tuple; coordinates of maximum value of ccm with subpixel precision
32        """
33        y_max, x_max = np.unravel_index(self.slice_ccm.argmax(), self.slice_ccm.shape)
34        minimizer = minimize(self.get_interpolated_px_value, (y_max, x_max), method="Nelder-Mead", options={"maxiter": 1000})
35        return minimizer.x
class GetMaxOptimizer:
 7class GetMaxOptimizer(object):
 8    """
 9    Class GetMaxOptimizer, used to extract the maximum value from a cross correlation matrix with subpixel precision.
10    """
11
12    def __init__(self, slice_ccm) -> None:
13        """
14        Creates an instance of GetMaxOptimizer.
15        :param slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel
16        precision.
17        """
18        self.slice_ccm = slice_ccm
19
20    def get_interpolated_px_value(self, coords):
21        """
22        Method to be used for calculating the interpolated values of cross correlation matrices.
23        :param coords: tuple of coordinates.
24        :return: float; value of cross correlation matrix at given coordinates.
25        For minimizer reasons -> negatives values become positive and positive become negative.
26        """
27        return -interpolate(self.slice_ccm, coords[1], coords[0])
28
29    def get_max(self):
30        """
31        Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.
32        :return: tuple; coordinates of maximum value of ccm with subpixel precision
33        """
34        y_max, x_max = np.unravel_index(self.slice_ccm.argmax(), self.slice_ccm.shape)
35        minimizer = minimize(self.get_interpolated_px_value, (y_max, x_max), method="Nelder-Mead", options={"maxiter": 1000})
36        return minimizer.x

Class GetMaxOptimizer, used to extract the maximum value from a cross correlation matrix with subpixel precision.

GetMaxOptimizer(slice_ccm)
12    def __init__(self, slice_ccm) -> None:
13        """
14        Creates an instance of GetMaxOptimizer.
15        :param slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel
16        precision.
17        """
18        self.slice_ccm = slice_ccm

Creates an instance of GetMaxOptimizer.

Parameters
  • slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel precision.
slice_ccm
def get_interpolated_px_value(self, coords):
20    def get_interpolated_px_value(self, coords):
21        """
22        Method to be used for calculating the interpolated values of cross correlation matrices.
23        :param coords: tuple of coordinates.
24        :return: float; value of cross correlation matrix at given coordinates.
25        For minimizer reasons -> negatives values become positive and positive become negative.
26        """
27        return -interpolate(self.slice_ccm, coords[1], coords[0])

Method to be used for calculating the interpolated values of cross correlation matrices.

Parameters
  • coords: tuple of coordinates.
Returns

float; value of cross correlation matrix at given coordinates. For minimizer reasons -> negatives values become positive and positive become negative.

def get_max(self):
29    def get_max(self):
30        """
31        Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.
32        :return: tuple; coordinates of maximum value of ccm with subpixel precision
33        """
34        y_max, x_max = np.unravel_index(self.slice_ccm.argmax(), self.slice_ccm.shape)
35        minimizer = minimize(self.get_interpolated_px_value, (y_max, x_max), method="Nelder-Mead", options={"maxiter": 1000})
36        return minimizer.x

Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.

Returns

tuple; coordinates of maximum value of ccm with subpixel precision